package com.study;

import java.io.*;
import java.util.*;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        test05();
    }
    public static void test01(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("e:/xx.txt");
            fos = new FileOutputStream("e:/e.txt");

            byte[] buffer = new byte[7];
            int count;
            while((count = fis.read(buffer)) != -1){
                fos.write(buffer, 0 , count);
                System.out.println("读到了" + count + "个");
            };

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static  void test02(){
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            isr = new InputStreamReader(new FileInputStream("d:/d.txt"));
            osw = new OutputStreamWriter(new FileOutputStream("e:/e.txt"));

            char[] buffer = new char[5];
            int read;
            while ((read = isr.read(buffer)) != -1){
                System.out.println(new String(buffer,0,read));
                osw.write(buffer, 0, read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void test03() throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("e:/xx.txt"),"GBK");
        osw.write("我擦！");
        osw.flush();
        osw.close();
    }

    public static void test04() throws IOException {
        FileInputStream fr = new FileInputStream("d:/d.txt");
        int res;
        while((res = fr.read()) != -1){
            System.out.println((char)res);
        }
    }
    public static void test05(){
        List<String> arrayList = new ArrayList<String>();
        arrayList.add("3.这是第三条数据");
        arrayList.add("5.这是第五条数据");
        arrayList.add("1.这是第一条数据");
        arrayList.add("4.这是第四条数据");
        arrayList.add("2.这是第二条数据");

//        Collections.sort(arrayList);
        Collections.sort(arrayList, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.charAt(0) - o2.charAt(0);
            }
        });
        System.out.println(arrayList);



//        Object[] lists = arrayList.toArray();
//        Arrays.sort(lists);

//        for(Object item : lists){
//            String sItem = (String) item;
//            System.out.println(sItem);
//        }
    };






}
